package Sync;

import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class InteractiveLogic {
    //手机端的子文件夹信息
    private List<String> subDirs_phone;
    //电脑端要求手机端要新建的文件夹,这里面封装的路径：/作为分割符号的
    private List<String> NewSubDirsToPhone;

    //是否是第一次同步 默认为true
    private Boolean IsFistSync;
    // 正在同步的文件夹的路径
    private String  SyncingFolder;


    // 手机端获取的目录文件详情
    private List<FileDetails> phoneFileDetailsList;
    // 手机端要发送要进行内容同步的文件
    private List<FileDetails> FileContentSync;
    // 手机端要发送新增文件
    private List<FileDetails> FileAdditionSync;
    //手机端要保存的编辑时间比较旧的文件
    private List<FileDetails> FileSaveSync;
    //手机端要发送新增文件数量
    private int FileAdditionSync_Count;
    //手机端要发送内容同步的文件数量
    private int FileContentSync_Count;


    //电脑端要进行发送新增文件
    private List<File> FileAdditionSync_PC;
    //电脑端要进行发送内容同步的文件
    private List<File> FileContentSync_PC;
    //电脑端要保存的编辑时间比较旧的文件
    private List<File> FileSaveSync_PC;
    //电脑端要发送新增文件数量
    private int FileAdditionSync_PC_Count;
    //电脑端要发送内容同步的文件数量
    private int FileContentSync_PC_Count;



    // 空参构造函数
    public InteractiveLogic() {
        this.subDirs_phone = new ArrayList<>();
        this.NewSubDirsToPhone = new ArrayList<>();
        this.IsFistSync = true;
        this.SyncingFolder = "云同步根目录";
        this.phoneFileDetailsList = new ArrayList<>();
        this.FileContentSync = new ArrayList<>();
        this.FileAdditionSync = new ArrayList<>();
        this.FileSaveSync = new ArrayList<>();
        this.FileAdditionSync_PC = new ArrayList<>();
        this.FileContentSync_PC = new ArrayList<>();
        this.FileSaveSync_PC = new ArrayList<>();
        this.FileAdditionSync_PC_Count = 0;
        this.FileAdditionSync_Count = 0;
        this.FileContentSync_PC_Count = 0;
        this.FileContentSync_Count = 0;
    }
    /**
     * 拷贝另一个InteractiveLogic对象的所有值到当前对象
     * @param source 源对象
     */
    public void copyFrom(InteractiveLogic source) {
        // 复制基本类型和Boolean
        this.IsFistSync = source.IsFistSync;
        this.SyncingFolder = source.SyncingFolder;
        this.FileAdditionSync_Count = source.FileAdditionSync_Count;
        this.FileContentSync_Count = source.FileContentSync_Count;
        this.FileAdditionSync_PC_Count = source.FileAdditionSync_PC_Count;
        this.FileContentSync_PC_Count = source.FileContentSync_PC_Count;

        // 复制字符串列表
        if (source.subDirs_phone != null) {
            this.subDirs_phone = new ArrayList<>(source.subDirs_phone);
        } else {
            this.subDirs_phone = new ArrayList<>();
        }

        if (source.NewSubDirsToPhone != null) {
            this.NewSubDirsToPhone = new ArrayList<>(source.NewSubDirsToPhone);
        } else {
            this.NewSubDirsToPhone = new ArrayList<>();
        }

        // 复制FileDetails列表
        if (source.phoneFileDetailsList != null) {
            this.phoneFileDetailsList = new ArrayList<>(source.phoneFileDetailsList);
        } else {
            this.phoneFileDetailsList = new ArrayList<>();
        }

        if (source.FileContentSync != null) {
            this.FileContentSync = new ArrayList<>(source.FileContentSync);
        } else {
            this.FileContentSync = new ArrayList<>();
        }

        if (source.FileAdditionSync != null) {
            this.FileAdditionSync = new ArrayList<>(source.FileAdditionSync);
        } else {
            this.FileAdditionSync = new ArrayList<>();
        }

        if (source.FileSaveSync != null) {
            this.FileSaveSync = new ArrayList<>(source.FileSaveSync);
        } else {
            this.FileSaveSync = new ArrayList<>();
        }

        // 复制File列表
        if (source.FileAdditionSync_PC != null) {
            this.FileAdditionSync_PC = new ArrayList<>(source.FileAdditionSync_PC);
        } else {
            this.FileAdditionSync_PC = new ArrayList<>();
        }

        if (source.FileContentSync_PC != null) {
            this.FileContentSync_PC = new ArrayList<>(source.FileContentSync_PC);
        } else {
            this.FileContentSync_PC = new ArrayList<>();
        }

        if (source.FileSaveSync_PC != null) {
            this.FileSaveSync_PC = new ArrayList<>(source.FileSaveSync_PC);
        } else {
            this.FileSaveSync_PC = new ArrayList<>();
        }
    }
    // ... existing code ...
    public List<String> getSubDirs_phone() {
        return subDirs_phone;
    }

    public void setSubDirs_phone(List<String> subDirs_phone) {
        this.subDirs_phone = subDirs_phone;
    }

    public List<String> getNewSubDirsToPhone() {
        return NewSubDirsToPhone;
    }

    public void setNewSubDirsToPhone(List<String> newSubDirsToPhone) {
        NewSubDirsToPhone = newSubDirsToPhone;
    }
    public Boolean getIsFistSync() {
        return IsFistSync;
    }

    public void setIsFistSync(Boolean IsFistSync) {
        this.IsFistSync = IsFistSync;
    }

    public String getSyncingFolder() {
        return SyncingFolder;
    }

    public void setSyncingFolder(String SyncingFolder) {
        this.SyncingFolder = SyncingFolder;
    }

    public List<FileDetails> getFileContentSync() {
        return FileContentSync;
    }

    public void setFileContentSync(List<FileDetails> fileContentSync) {
        FileContentSync = fileContentSync;
    }


    public List<FileDetails> getPhoneFileDetailsList() {
        return phoneFileDetailsList;
    }

    public void setPhoneFileDetailsList(List<FileDetails> phoneFileDetailsList) {
        this.phoneFileDetailsList = phoneFileDetailsList;
    }
    public List<FileDetails> getFileSaveSync() {
        return FileSaveSync;
    }

    public void setFileSaveSync(List<FileDetails> fileSaveSync) {
        FileSaveSync = fileSaveSync;
    }

    public List<File> getFileAdditionSync_PC() {
        return FileAdditionSync_PC;
    }

    public void setFileAdditionSync_PC(List<File> fileAdditionSync_PC) {
        FileAdditionSync_PC = fileAdditionSync_PC;
    }

    public List<File> getFileContentSync_PC() {
        return FileContentSync_PC;
    }

    public void setFileContentSync_PC(List<File> fileContentSync_PC) {
        FileContentSync_PC = fileContentSync_PC;
    }

    public List<File> getFileSaveSync_PC() {
        return FileSaveSync_PC;
    }

    public void setFileSaveSync_PC(List<File> fileSaveSync_PC) {
        FileSaveSync_PC = fileSaveSync_PC;
    }

    public int getFileAdditionSync_PC_Count() {
        return FileAdditionSync_PC_Count;
    }

    public void setFileAdditionSync_PC_Count(int fileAdditionSync_PC_Count) {
        FileAdditionSync_PC_Count = fileAdditionSync_PC_Count;
    }

    public int getFileAdditionSync_Count() {
        return FileAdditionSync_Count;
    }

    public void setFileAdditionSync_Count(int fileAdditionSync_Count) {
        FileAdditionSync_Count = fileAdditionSync_Count;
    }

    public int getFileContentSync_PC_Count() {
        return FileContentSync_PC_Count;
    }

    public void setFileContentSync_PC_Count(int fileContentSync_PC_Count) {
        FileContentSync_PC_Count = fileContentSync_PC_Count;
    }

    public int getFileContentSync_Count() {
        return FileContentSync_Count;
    }

    public void setFileContentSync_Count(int fileContentSync_Count) {
        FileContentSync_Count = fileContentSync_Count;
    }
    public List<FileDetails> getFileAdditionSync() {
        return FileAdditionSync;
    }

    public void setFileAdditionSync(List<FileDetails> fileAdditionSync) {
        FileAdditionSync = fileAdditionSync;
    }
    /**
     * 比较两个列表中的文件，根据文件大小和修改时间决定同步策略
     * @param fileList 文件列表
     * @param fileDetailsList 文件详情列表
     */
    public void compareAndCategorizeFiles(List<File> fileList, List<FileDetails> fileDetailsList) {
        // 清空之前的列表
        FileContentSync.clear();
        FileSaveSync.clear();
        FileContentSync_PC.clear();
        FileSaveSync_PC.clear();

        for (File file : fileList) {
            for (FileDetails fileDetails : fileDetailsList) {
                // 检查文件名是否匹配
                if (file.getName().equals(fileDetails.getFileName())) {
                    // 如果文件大小相同，跳过
                    if (file.length() == fileDetails.getFileSize()) {
                        break; // 跳过当前文件
                    }

                    // 文件大小不同，比较修改时间
                    long fileLastModified = file.lastModified();

                    // 解析FileDetails中的时间字符串为时间戳进行比较
                    // 注意：这里假设FileDetails中的时间格式为"yyyy-MM-dd HH:mm:ss"
                    // 在实际应用中可能需要更精确的时间解析
                    long fileDetailsLastModified = fileDetails.getLastModifiedTime();

                    // 比较修改时间
                    if (fileDetailsLastModified > fileLastModified) {
                        // FileDetails中的文件更新，将其加入FileContentSync
                        FileContentSync.add(fileDetails);
                        // 对应的旧文件加入FileSaveSync_PC
                        FileSaveSync_PC.add(file);
                    } else if (fileLastModified > fileDetailsLastModified) {
                        // File中的文件更新，将其加入FileContentSync_PC
                        FileContentSync_PC.add(file);
                        // 对应的旧文件详情加入FileSaveSync
                        FileSaveSync.add(fileDetails);
                    }
                    break; // 找到匹配的文件后跳出内层循环
                }
            }


        }
        // 将FileContentSync_PC_Count和FileContentSync_Count设置为非零值
        FileContentSync_PC_Count = FileContentSync_PC.size();
        FileContentSync_Count = FileContentSync.size();
    }
    /**
     * 找出两个列表中的差集部分
     * @param fileList 文件列表
     * @param fileDetailsList 文件详情列表
     */
    public void findNonIntersectingFiles(List<File> fileList, List<FileDetails> fileDetailsList) {
        // 创建临时列表存储交集文件名
        List<String> intersectingFileNames = new ArrayList<>();

        // 找出同名文件（交集部分）
        for (File file : fileList) {
            for (FileDetails fileDetails : fileDetailsList) {
                if (file.getName().equals(fileDetails.getFileName())) {
                    intersectingFileNames.add(file.getName());
                    break;
                }
            }
        }

        // 清空原有的列表
        FileAdditionSync_PC.clear();
        FileAdditionSync.clear();

        // 将 fileList 中非交集部分赋值给 FileAdditionSync_PC
        for (File file : fileList) {
            if (!intersectingFileNames.contains(file.getName())) {
                FileAdditionSync_PC.add(file);
            }
        }

        // 将 fileDetailsList 中非交集部分赋值给 FileAdditionSync
        for (FileDetails fileDetails : fileDetailsList) {
            if (!intersectingFileNames.contains(fileDetails.getFileName())) {
                FileAdditionSync.add(fileDetails);
            }
        }

        // 更新计数器
        FileAdditionSync_PC_Count = FileAdditionSync_PC.size();
        FileAdditionSync_Count = FileAdditionSync.size();
    }

    /**
     * 清除手机端获取的目录文件详情列表和电脑端要保存的编辑时间比较旧的文件列表
     */
    public void clearPhoneFileDetailsAndSaveSyncPC() {
        if (phoneFileDetailsList != null) {
            phoneFileDetailsList.clear();
        }
        if (FileSaveSync_PC != null) {
            FileSaveSync_PC.clear();
        }
    }
}
